<?php

namespace app\admin\model;

use think\Model;

class GPSDetail extends Model
{   
    /**
     * 采集信息列表 
     * Author Raven
     * Date 2018-10-18
     * Params [params]
     * @param  integer $category_id [分类id]
     */
    public function index($category_id = 0)
    {
        $categoryInfo = $this->getCategoryInfoByCategoryId($category_id);

        $itemLimit = 5;
        $categoryItem = $this->getCategoryItemByCategoryId($category_id, $itemLimit);

        if(empty($categoryInfo)){
            echo "<h1>分类信息查询失败</h1>";
            exit;
        }

        $categoryItem = $this->mregeBaseColumn($categoryItem);

        $tableName = $categoryInfo['category_name'];
        $tableHead = $this->createTableHead($categoryItem);
        
        $dbTabName = sprintf(GPSMigration::TABLE_PREFIX, $categoryInfo['hex_name']);
        $colomn = array_column($categoryItem, 'item_hex');

        $page_size = 20;
        $data = $this->getTableData($dbTabName, $colomn, $page_size);

        $res = [
            'category_id' => $category_id,
            'table_name' => $tableName,
            'table_head' => $tableHead,
            'colomn' => $colomn,
            'data' => $data,
        ];

        return $res;
    }

    /**
     * 采集信息详情 
     * Author Raven
     * Date 2018-10-18
     * Params [params]
     * @param  integer $category_id [分类id]
     * @param  integer $detail_id   [详情id]
     */
    public function detail($category_id = 0, $detail_id = 0)
    {
        $categoryInfo = $this->getCategoryInfoByCategoryId($category_id);

        $itemLimit = 100;
        $categoryItem = $this->getCategoryItemByCategoryId($category_id, $itemLimit);

        if(empty($categoryInfo)){
            echo "<h1>分类信息查询失败</h1>";
            exit;
        }

        $categoryItem = $this->mregeBaseColumn($categoryItem);
        $categoryItem = $this->formatCategoryItemData($categoryItem);

        $dbTabName = sprintf(GPSMigration::TABLE_PREFIX, $categoryInfo['hex_name']);
        $colomn = array_column($categoryItem, 'item_hex');

        $data = $this->getTableDetail($dbTabName, $colomn, $detail_id);


        foreach ($categoryItem as $key => $value) {
            if($value['item_type'] == 3){
                $data[$value['item_hex']] = str_replace("|", ",", $data[$value['item_hex']]);
            }
        }
        $res = array(
            'item' => $categoryItem,
            'data' => $data
        );

        return $res;
    }

    /**
     * 更新采集信息详情 
     * Author Raven
     * Date 2018-10-20
     * Params [params]
     * @param  integer $category_id [分类id]
     * @param  integer $detail_id   [详情id]
     * @param  array   $data        [保存的数据对象]
     */
    public function updateDetail($category_id = 0, $detail_id = 0, $data = [])
    {
        $categoryInfo = $this->getCategoryInfoByCategoryId($category_id);

        $dbTabName = sprintf(GPSMigration::TABLE_PREFIX, $categoryInfo['hex_name']);
        $res = $this->updateTableDetail($dbTabName, $detail_id, $data);

        return $res;
    }

    /**
     * 更新详情信息 
     * Author Raven
     * Date 2018-10-20
     * Params [params]
     * @param  string  $table_name [数据表名称]
     * @param  integer $detail_id  [数据id]
     * @param  array   $data       [保存的数据对象]
     */
    public function updateTableDetail($table_name = '', $detail_id = 0, $data = [])
    {
        $GPSMigration = new GPSMigration();

        $res = $GPSMigration
            ->updateTableDetail($table_name, $detail_id, $data);

        return $res;
    }

    /**
     * 格式化分类信息 
     * Author Raven
     * Date 2018-10-20
     * Params [params]
     * @param  array  $category_item [分类对象]
     */
    public function formatCategoryItemData($category_item = [])
    {
        foreach ($category_item as $key => $value) {
            if(in_array($value['item_type'], [2, 3])){
                $category_item[$key]['item_value'] = explode("|", $value['item_value']);
            }
        }

        return $category_item;
    }

    /**
     * 获取分类详情 
     * Author Raven
     * Date 2018-10-18
     * Params [params]
     * @param integer $category_id [分类id]
     */
    public function getCategoryInfoByCategoryId($category_id = 0)
    {
        $categoryInfo = GPSCategory::where('id', $category_id)
            ->field(['id', 'category_name', 'hex_name'])
            ->find()
            ->toArray();
        return $categoryInfo;
    }

    /**
     * 获取分类字段列表 
     * Author Raven
     * Date 2018-10-18
     * Params [params]
     * @param  integer $category_id [分类id]
     * @param  integer $limit       [获取条数]
     */
    public function getCategoryItemByCategoryId($category_id = 0, $limit = 0)
    {
        $itemInfo = GPSCategoryItem::where('fk_category_id', $category_id)
            ->where('status = 1')
            ->field((['id', 'item_name', 'item_type', 'item_value', 'item_hex', 'status']))
            ->order('item_order', 'asc')
            ->limit(5)
            ->select()
            ->toArray();

        return $itemInfo;
    }

    /**
     * 拼装默认字段 
     * Author Raven
     * Date 2018-10-18
     * Params [params]
     * @param  array  $item_info [字段列表]
     */
    public function mregeBaseColumn($item_info = [])
    {
       $res = array_merge(
            [['item_name' => '#', 'item_hex' => 'id']],
            $item_info,
            [['item_name' => '经度', 'item_hex' => 'data_lng']],
            [['item_name' => '维度', 'item_hex' => 'data_lat']],
            [['item_name' => '状态', 'item_hex' => 'status']],
            [['item_name' => '创建时间', 'item_hex' => 'create_time']],
            [['item_name' => '更新时间', 'item_hex' => 'update_time']]
        );

       return $res;
    }

    /**
     * 创建表头 
     * Author Raven
     * Date 2018-10-18
     * Params [params]
     * @param  array  $item_info [字段列表]
     */
    public function createTableHead($item_info = [])
    {
        $res = [];

        foreach ($item_info as $key => $value) {
            $res[] = $value['item_name'];
        }

        return $res;
    }

    /**
     * 获取表格数据 
     * Author Raven
     * Date 2018-10-18
     * Params [params]
     * @param  string  $table_name [数据表名称]
     * @param  array   $field      [查询的字段]
     * @param  integer $page_size  [每页记录条数]
     */
    public function getTableData($table_name = '', $field = [], $page_size = 20)
    {
        $GPSMigration = new GPSMigration();

        $res = $GPSMigration
            ->getTableData($table_name, $field, $page_size);

        return $res;
    }

    /**
     * 获取详情信息 
     * Author Raven
     * Date 2018-10-18
     * Params [params]
     * @param  string  $table_name [数据表名称]
     * @param  array   $field      [查询的字段]
     * @param  integer $detail_id  [数据id]
     */
    public function getTableDetail($table_name = '', $field = [], $detail_id = 0)
    {
        $GPSMigration = new GPSMigration();

        $res = $GPSMigration
            ->getTableDetail($table_name, $field, $detail_id);

        return $res;
    }
}
